<script setup lang="ts">
import { inject, ref, computed, watchEffect } from "vue";
import type { FormDesignerComponents, FormItemPerm } from "../FormComponentMixin";
import { isReadonly } from "../FormItem";
const props = defineProps({
  type: {
    type: String,
    default: ""
  },
  mode: {
    type: String,
    default: "normal" //组件模式，free设计模式，normal 编辑模式, read 阅读模式，disable 禁用模式
  }
});

const _value = defineModel<string>();
const config = defineModel<FormDesignerComponents>("config");
const perm = inject<Array<FormItemPerm>>("perm");
const multipleValue = ref([]);

const disabled = computed(() => {
  if (props.mode === "disable" && isReadonly(perm, config.value.code)) {
    return true;
  }
  return false;
});

const handleValueChange = val => {
  _value.value = val.join(",");
};

watchEffect(() => {
  if (_value.value) {
    multipleValue.value = _value.value.split(",");
  }
});
</script>

<template>
  <el-checkbox-group
    v-if="config.props.expanding"
    v-model="multipleValue"
    :disabled="disabled"
    @change="handleValueChange"
  >
    <el-checkbox
      v-for="(op, i) in config.props.options"
      :key="i"
      :label="op.label"
      :value="op.value"
    />
  </el-checkbox-group>
  <el-select
    v-else
    v-model="multipleValue"
    multiple
    :disabled="disabled"
    @change="handleValueChange"
  >
    <el-option
      v-for="(op, i) in config.props.options"
      :key="i"
      :label="op.label"
      :value="op.value"
    />
  </el-select>
</template>

<style scoped></style>
